/**
 
 * 
 */
package nl.boektrust.btsoep.server;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

/**
 * @author rao3
 *
 * the implementation of the server that can serve XML requests to Koppelvlak bookstores. 
 */
public class SendMessage extends HttpServlet {
	private boolean isObjectifyServiceRegistered = false;
	private ServletContext context;

	
	ServletContext getContext() {
		return this.context;
	}

	public void init() throws ServletException {
		context = getServletContext();
		//System.err.println("Real Path = " + context.getRealPath("/"));
	}

	/**
	 * 
	 */
	public SendMessage() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param delegate
	 */
	public SendMessage(Object delegate) {
		//super(delegate);
		// TODO Auto-generated constructor stub
	}
	
	
	 /* 
	   * play function to send a message using XMPP 
	   */
	  void sendMessage(String st) {
		  if (st == null || st.length() == 0) return;

		  JID[] jids = new JID[5];
		  jids[0] = new JID("roelanto@gmail.com");
		  jids[1] = new JID("boektrust@gmail.com");
		  Message msg = new MessageBuilder()
		  .withRecipientJids(jids[0],jids[1])
		  .withBody(st)
		  .build();

		  boolean messageSent = false;
		  XMPPService xmpp = XMPPServiceFactory.getXMPPService();
		  SendResponse status = xmpp.sendMessage(msg);
		  messageSent = (status.getStatusMap().get(jids) == SendResponse.Status.SUCCESS);

		  if (!messageSent) {
			  System.err.println("Couldn't send message "+st);
			  try {
				  if (status.getStatusMap().get(jids) == SendResponse.Status.INVALID_ID) {
					  System.err.println("Status:  INVALID_ID"); 

				  } else if (status.getStatusMap().get(jids) == SendResponse.Status.OTHER_ERROR) {
					  System.err.println("Status:  OTHER_ERROR"); 
					  
				  } else if (status.getStatusMap().get(jids) == SendResponse.Status.SUCCESS){
					  System.err.println("Status: SUCCESS");
				  }
			  } catch (Exception e) {
				  e.printStackTrace();
			  }
			  // do nothing
		  }
	  }
	public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException  
	{
		
		/*
		 * get raw XML from the inputstream. 
		 */
		System.err.println("task sendMessage POST");
		System.err.println("paramter key is "+request.getParameter("message"));
		String parameter = request.getParameter("message");
		sendMessage(parameter);
	} 
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		System.err.println("task sendMessage GET");
		System.err.println("paramter key is "+request.getParameter("message"));
		String parameter = request.getParameter("message");
		sendMessage(parameter);
	
	}


}
